<template>
  <div class="app-container">
    <el-card shadow="never">
      <!-- 搜索 -->
      <div class="topnav">
        <div class="topleft">
          <span class="subjectname">学科名称</span>
          <el-input class="subjectinp" v-model="query.subjectName"></el-input>
          <el-button @click="onCleanWord">清除</el-button>
          <el-button type="primary" @click="getSubjectList()">搜索</el-button>
        </div>
        <div class="topright">
          <el-button
            class="addsubjectright"
            type="success"
            @click="dialogFormVisible = true"
            ><i class="el-icon-edit"></i>新增学科</el-button
          >
        </div>
      </div>
      <!-- 信息提示条 -->
      <el-alert
        v-if="alertText !== ''"
        :title="alertText"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      ></el-alert>
      <!-- 表格 -->
      <el-table :data="data.items" style="width: 100%" v-loading="listLoading">
        <el-table-column type="index" label="序号" width="100">
        </el-table-column>
        <el-table-column prop="subjectName" label="学科名称" width="150">
        </el-table-column>
        <el-table-column prop="username" label="创建者" width="180">
        </el-table-column>
        <el-table-column label="创建日期" width="200">
          <template slot-scope="scope">
            <span>{{
              scope.row.addDate | parseTimeByString("{y}-{m}-{d}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="前台是否显示" width="100">
          <template slot-scope="scope">
            {{ scope.row.isFrontDisplay === 1 ? "是" : "否" }}
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录" width="100">
        </el-table-column>
        <el-table-column prop="tags" label="标签" width="100">
        </el-table-column>
        <el-table-column prop="totals" label="题目数量" width="100">
        </el-table-column>
        <el-table-column prop="caozuo" label="操作" width="380">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="classify(scope.row)"
              >学科分类</el-button
            >
            <el-button
              type="success"
              size="mini"
              @click="
                $router.push({
                  name: 'subjects-tags',
                  query: {
                    id: 'this.scope.row.id',
                    name: 'this.scope.row.subjectName',
                  },
                })
              "
              >学科标签</el-button
            >
            <el-button type="info" size="mini" @click="showSubject(scope.row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="delectSubject(scope.row)"
              >{{ $t("table.delete") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 页码条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="query.pagesize"
        layout="prev,pager,next,sizes,jumper"
        :total="data.counts"
      >
      </el-pagination>
      <!-- 新增弹出框 -->
      <el-dialog
        title="新增学科"
        :visible.sync="dialogFormVisible"
        width="30%"
        :before-close="handleClose"
      >
        <br />
        <el-form
          :model="add"
          :rules="rules"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="学科名称" prop="subjectName">
            <el-input v-model="add.subjectName"></el-input>
          </el-form-item>
        </el-form>
        <span class="addleftword">是否显示:</span>
        <el-switch
          v-model="value"
          active-color="#13ce66"
          inactive-color="#ff4949"
        >
        </el-switch>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addSubject">确 定</el-button>
        </div>
      </el-dialog>
      <!--修改弹出框  -->
      <el-dialog
        title="修改学科"
        :visible.sync="updatedialogFormVisible"
        width="30%"
        :before-close="handleClose"
      >
        <br />
        <el-form
          :model="update"
          :rules="rules"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="学科名称" prop="subjectName">
            <el-input v-model="update.subjectName"></el-input>
          </el-form-item>
        </el-form>
        <span class="addleftword">是否显示:</span>
        <el-switch
          v-model="value"
          active-color="#13ce66"
          inactive-color="#ff4949"
        >
        </el-switch>
        <div slot="footer" class="dialog-footer">
          <el-button @click="updatedialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="editSubject">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 删除 -->
    </el-card>
  </div>
</template>
<script>
import { list as subjectList, add as addsubject, update as editsubject, remove as deletesubject, detail as detailsubject } from '@/api/hmmm/subjects.js'

export default {
  name: 'subjects',
  components: {

  },
  data () {
    return {
      alertText: '',
      data: [],
      value: true,
      loading: false,
      listLoading: true,
      query: {
        // 当前页数
        page: 1,
        // 每页显示的数据条数
        pagesize: 10,
        // 科目名称
        subjectName: '',
        total: 0
      },
      add: {
        subjectName: '',
        isFrontDisplay: 1
      },
      dialogFormVisible: false,
      ruleForm: { name: '' },
      rules: {
        subjectName: [
          { required: true, message: '请输入课程名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ]
      },
      updatedialogFormVisible: false,
      update: {
        subjectName: '',
        id: '',
        isFrontDisplay: ''
      },
      detail: {
        id: '',
        name: ''
      }
    }
  },
  mounted () {
    this.getSubjectList()
  },
  created () {

  },
  methods: {
    //   获取学科列表
    async getSubjectList () {
      this.listLoading = true
      try {
        const { data } = await subjectList(this.query)
        console.log(this.query.subjectName)
        this.data = data
        this.alertText = `共${data.counts}条记录`
        this.$message.success('获取成功')
        this.listLoading = false
      } catch (error) {
        this.$message.error('获取失败')
        console.log(error)
      }
    },
    // 页码条数改变时
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.query.pagesize = val
      this.getSubjectList()
    },
    // 当前页码改变时
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.query.page = val
      this.getSubjectList()
    },
    // 清除文本框内容
    onCleanWord () {
      this.query.subjectName = ''
    },
    // 新增弹出框
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    // 新增
    async addSubject () {
      try {
        if (this.value === true) {
          this.add.isFrontDisplay = 1
        } else {
          this.add.isFrontDisplay = 0
        }
        await addsubject(this.add)
        // 将数据添加到列表
        // this.data.unshift(config.data)
        this.getSubjectList()
        this.$message.success('增加成功')
        this.add.subjectName = ''
        this.add.isFrontDisplay = 1
      } catch (error) {
        console.log(error)
      }
      this.dialogFormVisible = false
    },
    // 修改
    async showSubject (row) {
      this.updatedialogFormVisible = true
      this.update.id = row.id
      this.update.subjectName = row.subjectName
      this.update.isFrontDisplay = row.isFrontDisplay
    },
    async editSubject () {
      try {
        await editsubject(this.update)
        this.updatedialogFormVisible = false
        this.getSubjectList()
      } catch (error) {
        console.log(error)
      }
    },
    // 删除
    delectSubject (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // this.deletesubject()
        await deletesubject({ id: row.id })
        this.getSubjectList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
        .catch(() => {
          this.$message.info('已取消操作!')
        })
    },
    async removeSubject () {
      try {
        const data = await deletesubject(this.delete.id)
        console.log(data)
      } catch (error) {
        console.log(error)
      }
    },
    // 学科详情
    classify (row) {
      this.detail.id = row.id
      this.detail.name = row.subjectName
      this.$router.push({
        name: 'subjects-directorys',
        query: {
          id: 'this.detail.id',
          name: 'this.detail.subjectName'
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="less" scoped>
.topnav {
  display: flex;
  justify-content: space-between;
  width: 100%;
}
.subjectname {
  font-size: 20px;
  font-family: "楷体";
  font-weight: 700;
}
.subjectinp {
  width: 300px;
  margin: auto 18px;
}
.el-button {
  height: 42px;
}

.alert {
  margin-top: 15px;
  font-size: 14px;
}
.el-table {
  margin-top: 15px;
}
.el-pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
}
.addleftword {
  font-size: 18px;
  font-weight: 700;
}
.el-switch {
  margin-left: 15px;
}
/deep/.el-form-item label {
  font-size: 18px;
  margin-left: -15px;
}
</style>
